package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.etoak.common.core.excption.CustomExcetpion;
import com.etoak.common.core.vo.PageVO;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.DictService;
import com.etoak.service.DishesService;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etoak
 * @since 2024-12-18
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {

    @Autowired
    DishesService dictService;

    // 添加食材
    @Override
    public void add(Ingredients ingredients) {

        // 判断食材名称是否重复
        if (ObjectUtils.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomExcetpion("食材名称重复!");
        }

        // 设置食材创建时间
        ingredients.setCreateTime(DateUtil.now());
        this.save(ingredients);
    }

    @Override
    public PageVO<Ingredients> getList(int pageNum, int pageSize,Ingredients ingredients) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ingredients> list = lambdaQuery().likeRight(StringUtils.isNotEmpty(ingredients.getName()), Ingredients::getName, ingredients.getName())
                .eq(StringUtils.isNotEmpty(ingredients.getType()), Ingredients::getType, ingredients.getType())
                .list();
        PageInfo<Ingredients> pageInfo = new PageInfo(list);
        return new PageVO<>(pageInfo.getTotal(),list);
    }

    @Override
    public void update(int id, Ingredients ingredients) {

        // 判断该食材是否为空 为空就不更新
        if (ObjectUtils.isEmpty(getById(id))){
            throw new CustomExcetpion("该食材不存在!");
        }

        /*
        * 判断食材名称是否重复
        * 1.判断食材名称是否为空 为空就不更新
        * 2.判断食材名称是否和原来的重复
        * 3.判断食材名称是否和原来的不同
        */
        String name = ingredients.getName();
        if (ObjectUtils.isNotEmpty(name)) {
            Ingredients byName = getByName(name);
            if (ObjectUtils.isNotEmpty(byName)&&!byName.getId().equals(id)){
                throw new CustomExcetpion("食材名称重复!");
            }
        }

        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);
    }

    @Override
    public void delete(int id) {

        // 判断该食材是否为空 为空就不更新
        if (ObjectUtils.isEmpty(getById(id))){
            throw new CustomExcetpion("该食材不存在!");
        }

        // 判断食材有没有被使用
        if (dictService.isIngredientsUsed(id)){
            throw new CustomExcetpion("食材被使用不能删除!");
        }
        this.removeById(id);
    }

    @Override
    public List<Ingredients> listByType(String type) {
        /**
         * 1.根据类型查询食材列表
         *  Ingredients ingredients = new Ingredients();
         *  ingredients.setType(type);
         *  return baseMapper.getList(ingredients);
         */
        return lambdaQuery().eq(Ingredients::getType,type).list();
    }

    private Ingredients getByName(String name) {
        return lambdaQuery().eq(Ingredients::getName,name).one();
    }




}
